package com.guchenbo.example.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author guchenbo
 * @date 2024/10/22
 */
public class TestException {
    public static void main(String[] args) throws InterruptedException {
//        singleThread();

//        poolExecute();

        poolSubmit();
    }

    static void singleThread()  {
        ExceptionThread exceptionThread = new ExceptionThread();
        exceptionThread.start();
    }

    static void poolExecute() throws InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 3; i++) {
            executorService.execute(new ExceptionThread());
        }
        while (true){
            System.out.println(executorService.isShutdown());
            Thread.sleep(3000);
        }
    }

//    @Test
static  void poolSubmit() {

        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (int i = 0; i < 3; i++) {
            Future<?> future = executorService.submit(new ExceptionThread());
            try {
                future.get();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    static class ExceptionThread extends Thread {

        @Override
        public void run() {
            System.out.println(Thread.currentThread().getName() + " start ");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            String s = null;
            System.out.println(s.getBytes());
            System.out.println(Thread.currentThread().getName() + " done ");
        }
    }
}
